package com.tanhua.server.controller;

import com.tanhua.server.service.CommentService;
import com.tanhua.server.service.MovementsMQService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Map;

@RestController
@RequestMapping("comments")
@Slf4j
public class CommentsController {

    @Autowired
    private CommentService commentService;
    @Autowired
    private MovementsMQService movementsMQService;

    /**
     * 接口名称：评论列表
     * 接口路径：GET/comments
     * 需求描述：分页查询评论，查询条件：动态id、评论类型
     */
    @GetMapping
    public ResponseEntity<Object> commentsList(String movementId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize){
        if (page<1) {
            page = 1; // 解决前端模拟器传入页码异常问题
        }
        return commentService.commentsList(movementId,page,pagesize);
    }

    /**
     * 接口名称：评论-提交
     * 接口路径：POST/comments
     */
    @PostMapping
    public ResponseEntity<Object> saveComments(@RequestBody Map<String,String> map) throws ParseException {
        // 获取请求参数：movementId、comment
        String movementId = map.get("movementId");
        String comment = map.get("comment");
        movementsMQService.disLikePublishMsg(movementId);
        return commentService.saveComments(movementId,comment);
    }

}
